# Create your views here.
from django.shortcuts import render, redirect, HttpResponse
from django.views import View
from .models import Press, Book, Author
from . import models
import time
from django.utils.decorators import method_decorator
import json
from django.http import JsonResponse
from django.urls import reverse


def home(request, year, month):
    return HttpResponse(reverse('app01:home', kwargs={'year': year, 'month': month}))
    # return render(request, 'home.html', locals())


def index(request, year, month):
    # return HttpResponse(reverse('index', args=(year, month)))
    return render(request, 'home.html', locals())


def press_list(request):
    press_obj = Press.objects.all().order_by('id')
    return render(request, 'list/press_list.html', locals())


def timer(fn):
    def inner(request, *args, **kwargs):
        '''执行前的操作, 计算初始时间'''
        start = time.time()

        '''函数执行, 并将返回值由变量ret保存'''
        ret = fn(request, *args, **kwargs)

        '''函数执行后的操作'''
        print('函数的执行时间是{}'.format(time.time() - start))

        '''返回函数的返回值'''
        return ret

    return inner


# @timer
# def add_press(request):
#     if request.method == 'POST':
#         name = request.POST.get('name')
#         Press.objects.create(name=name)
#         return redirect(reverse('press_list'))
#     return render(request, 'add/add_press2.html')


@method_decorator(timer, name='get')
@method_decorator(timer, name='post')
class AddPress(View):

    # @method_decorator(timer)
    def dispatch(self, request, *args, **kwargs):
        ret = super().dispatch(request, *args, **kwargs)
        return ret

    # @method_decorator(timer)
    def get(self, request):
        return render(request, 'add/add_press2.html')

    # @method_decorator(timer)
    def post(self, request):
        name = request.POST.get('name')
        Press.objects.create(name=name)
        return redirect(reverse('press'))


# def delete_press(request, delete_id):
#     # delete_id = request.GET.get('id')
#     print(delete_id)
#     Press.objects.filter(id=delete_id).delete()
#
#     return redirect(reverse('press_list'))


def edit_press(request, edit_id):
    press_obj = Press.objects.get(id=edit_id)
    print(press_obj)

    if request.method == 'POST':
        new_name = request.POST.get('name')
        press_obj.name = new_name
        press_obj.save()
        return redirect(reverse('press'))

    return render(request, 'edit/edit_press2.html', locals())


def book_list(request):
    book_obj = Book.objects.all()

    return render(request, 'list/book_list.html', locals())


def add_book(request):
    press_data = Press.objects.all()
    if request.method == 'POST':
        book_name = request.POST.get('book_name')
        press_id = request.POST.get('press_id')
        Book.objects.create(title=book_name, press_id=press_id)
        return redirect('/book_list/')

    return render(request, 'add/add_book.html', locals())


# def delete_book(request):
#     delete_id = request.GET.get('id')
#     Book.objects.filter(id=delete_id).delete()
#     return render(request, 'delete/delete_success.html')


def edit_book(request, edit_id):
    book_obj = Book.objects.get(id=edit_id)
    press_data = Press.objects.all()
    if request.method == 'POST':
        new_name = request.POST.get('book_name')
        press_id = request.POST.get('press_id')
        book_obj.title = new_name
        book_obj.press_id = press_id
        book_obj.save()
        return redirect('/book_list/')

    return render(request, 'edit/edit_book.html', locals())


def author_list(request):
    author_obj = Author.objects.all()
    return render(request, 'list/author_list.html', locals())


def add_author(request):
    book_list = Book.objects.all()

    if request.method == "POST":
        author_name = request.POST.get('author_name')
        book_ids = request.POST.getlist('book_ids')  # request.POST.get 只能获取一个值, request.POST.getlist 能获取多个值, 用列表保存
        author_obj = Author.objects.create(name=author_name)
        author_obj.books.add(*book_ids)  # 参数是一个一个单独的书籍id值
        # author_obj.books.set(book_ids)  # 参数是书籍id值的列表
        return redirect('/author_list/')

    return render(request, 'add/add_author.html', locals())


# def delete_author(request):
#     delete_id = request.GET.get('id')
#     Author.objects.filter(id=delete_id).delete()
#     return redirect('/author_list/')


def edit_author(request, edit_id):
    author_obj = Author.objects.get(id=edit_id)
    book_list = Book.objects.all()

    if request.method == 'POST':
        author_name = request.POST.get('author_name')
        book_ids = request.POST.getlist('book_ids')
        author_obj.name = author_name
        author_obj.save()
        author_obj.books.set(book_ids)

        return redirect(reverse('author'))

    return render(request, 'edit/edit_author.html', locals())


def test(request):
    return HttpResponse(json.dumps({'name': '魏新雨咋软', 'status': 200}, ensure_ascii=False))
    # return JsonResponse({'name': '魏新雨咋软', 'status': 200},  json_dumps_params={'ensure_ascii': False})


def delete(request, table: str, delete_id):
    print(table, delete_id)
    table_class = getattr(models, table.capitalize())
    print(table_class)
    table_class.objects.get(id=delete_id).delete()
    # table_class.objects.filter(id=delete_id).first().delete()

    return redirect(reverse(table))